package com.ocom.wxapi.controller;

import com.ocom.common.entity.human.EntityBizApply;
import com.ocom.common.entity.human.EntityBizLeaveApply;
import com.ocom.common.request.company.GetApplyTasksRequest;
import com.ocom.common.request.company.GetFlowApprovalDetailRequest;
import com.ocom.common.request.company.HandleApplyRequest;
import com.ocom.common.request.human.LeaveApplyDetailRequest;
import com.ocom.common.request.wx.AppApplyDetailRequest;
import com.ocom.common.request.wx.AppApplyHandleRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.GetFlowApprovalDetailVo;
import com.ocom.common.vo.human.LeaveApplyDetailVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.wxapi.client.CompanyClientService;
import com.ocom.wxapi.client.HumanClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/apply")
@RestController
public class FlowController {
    @Autowired
    private CompanyClientService companyClientService;

    @Autowired
    private HumanClientService humanClientService;

    @PostMapping("/appApplyList")
    public Result appApplyList(){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        GetApplyTasksRequest getApplyTasksRequest = new GetApplyTasksRequest();
        getApplyTasksRequest.setComId(yoCiUser.getComId());
        getApplyTasksRequest.setUserId(yoCiUser.getId());
//        getApplyTasksRequest.setComId(110l);
//        getApplyTasksRequest.setUserId(1411l);
        return this.companyClientService.getApplyTasks(getApplyTasksRequest);
    }


    @PostMapping("/deptApplyList")
    public Result deptApplyList(@RequestBody  EntityBizApply entityBizApply){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        entityBizApply.setComId(yoCiUser.getComId());
        if(yoCiUser.getRole()==23l){
            entityBizApply.setApplyDeptId(Long.valueOf(yoCiUser.getDeptId()));
        }
//        getApplyTasksRequest.setComId(110l);
//        getApplyTasksRequest.setUserId(1411l);
        return this.companyClientService.getApplyListByPid(entityBizApply);
    }

    @PostMapping("/appApplyDetail")
    public Result appApplyDetail(@RequestBody  AppApplyDetailRequest applyDetailRequest){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();

//        Long companyId = 110l;
        Long companyId = yoCiUser.getComId();
        Integer applyId = applyDetailRequest.getBusinessId();

        EntityBizLeaveApply entityBizLeaveApply=new EntityBizLeaveApply();
        entityBizLeaveApply.setId(applyId.longValue());
        entityBizLeaveApply.setComId(companyId);
        Result<EntityBizLeaveApply> leaveApplyResult = this.companyClientService.leavelApplyOne(entityBizLeaveApply);


        LeaveApplyDetailRequest leaveApplyDetailRequest = new LeaveApplyDetailRequest();
        leaveApplyDetailRequest.setId(applyId);
        leaveApplyDetailRequest.setComId(companyId);
        Result<LeaveApplyDetailVo> resultA= this.companyClientService.leaveApplyDetail(leaveApplyDetailRequest);

        GetFlowApprovalDetailRequest getFlowApprovalDetailRequest = new GetFlowApprovalDetailRequest();
        getFlowApprovalDetailRequest.setComId(companyId);
//        getFlowApprovalDetailRequest.setBusinessKey(applyDetailRequest.getActivitiKey());
        getFlowApprovalDetailRequest.setActivitiKey(leaveApplyResult.getData().getActivitiKey());
        getFlowApprovalDetailRequest.setDefKey(leaveApplyResult.getData().getDefKey());
        Result<List<GetFlowApprovalDetailVo>> getFlowApprovalDetailVoListResult= this.companyClientService.getFlowApprovalDetail(getFlowApprovalDetailRequest);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("rawData",resultA.getData().getRawData());
        resultMap.put("formatData",resultA.getData().getFormatData());
        resultMap.put("approvalData",getFlowApprovalDetailVoListResult.getData());
        return ResultUtil.success(resultMap);
    }

    @PostMapping("/appApplyHandle")
    public Result appApplyHandle(@RequestBody AppApplyHandleRequest appApplyHandleRequest){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//
//

        EntityBizLeaveApply entityBizLeaveApply=new EntityBizLeaveApply();
        entityBizLeaveApply.setComId(yoCiUser.getComId());
        entityBizLeaveApply.setId(appApplyHandleRequest.getBusinessId());
        Result<EntityBizLeaveApply>  result = companyClientService.leavelApplyOne(entityBizLeaveApply);

        if(result.getCode()!=200){
            return result;
        }

        HandleApplyRequest handleApplyRequest =new HandleApplyRequest();

        Map<String,Object> params =new HashMap<>();
        params.put("status" ,appApplyHandleRequest.getStatus());
        params.put("days",result.getData().getDays().intValue());
        params.put("type",result.getData().getType());

//        handleApplyRequest.setComId(110l);
//        handleApplyRequest.setUserId(1411l);
        handleApplyRequest.setComId(yoCiUser.getComId());
        handleApplyRequest.setUserId(yoCiUser.getId());
        handleApplyRequest.setParams(params);
        handleApplyRequest.setBusinessId(appApplyHandleRequest.getBusinessId());
        handleApplyRequest.setComment(appApplyHandleRequest.getComment());
        handleApplyRequest.setTaskId(appApplyHandleRequest.getTaskId());
        handleApplyRequest.setStatus(appApplyHandleRequest.getStatus());

        return this.companyClientService.handleApply(handleApplyRequest);
    }




}
